<template>
  <view class="mutual-assistance">
    <!-- #ifdef H5 -->
    <view class="card-design">
      <card :list-data="cardData" @on-click="onCard"></card>
    </view>
    <!-- #endif -->
    <view class="check-mutual"
      ><uni-data-checklist
        :wrap="true"
        @change="changeChecklist"
        :localdata="localdata"
        :value="checkValue"
        style="background-color: #f7f8fd; padding: 20rpx 0"
      >
      </uni-data-checklist
    ></view>
    <scroll-view
      v-if="nveilingInf.length > 0"
      class="nveiling-big-box"
      scroll-y="true"
      @scrolltolower="scrolltolower"
    >
      <nveilingCard
        v-for="(item, inedx) in nveilingInf"
        :title="item.title"
        :unveilingText="item.unveilingText"
        :unveilingAddress="item.unveilingAddress"
        :enterpriceUrl="item.enterpriceUrl"
        :enterpriceTitle="item.enterpriceTitle"
        :describeList="item.describeList"
        :showFlag="item.showFlag"
        :imageUrl="item.imageUrl"
        @getAtention="getAtention(item.publishPersonId)"
        @getDetailData="getDetail(item.publishPersonId)"
        :interestFlag="interestFlag"
      >
      </nveilingCard>
    </scroll-view>
    <none-data v-if="nveilingInf.length === 0 && isLoading" />
  </view>
</template>

<script setup>
// import MyTab from "@/components/tabsCopy/index.vue";
import {
  onNavigationBarButtonTap,
  onNavigationBarSearchInputChanged,
  onShow,
} from "@dcloudio/uni-app";
import nveilingCard from "./componts/nveiling-card.vue";

// #ifdef H5
import card from "@/components/card/index.vue";
// #endif
import billboard from "@/components/billboard/index.vue";
import uniDataChecklist from "@/uni_modules/uni-data-checkbox/components/uni-data-checkbox/uni-data-checkbox.vue";
import { ref, onMounted, watch } from "vue";

import {
  getXhBGroupByList,
  saveHxbUserOperation,
} from "@/api/mutual-assistance";
const interestFlag = ref(true);
const jbStatus = ref(2);
const checkValue = ref("");
const curPage = ref(1);
const totalPage = ref(1);
const searchValue = ref("");
const cardData = ref([
  {
    className: "design",
    title: "设计类",
  },
  {
    className: "develop",
    title: "开发类",
  },
  {
    className: "marketing",
    title: "营销类",
  },
  {
    className: "advice",
    title: "咨询类",
  },
  {
    className: "other",
    title: "其他",
  },
]);
const localdata = ref([
  {
    text: "我要揭榜",
    value: "我要揭榜",
  },
  {
    text: "已揭榜",
    value: "已揭榜",
  },
  {
    text: "已发榜",
    value: "已发榜",
  },
  {
    text: "已关注",
    value: "已关注",
  },
]);

const nveilingInf = ref([]);
const isLoading = ref(false);

function getDetail(data) {
  console.log(data, "data");
  uni.navigateTo({
    url: `/package_mutual_assistance/pages/mutual-assistance/detail?id=${data}&&status=${jbStatus.value}`,
  });
}

function changeChecklist(data) {
  console.log(data.detail.value);
  interestFlag.value = true;
  nveilingInf.value = [];
  curPage.value = 1;
  switch (data.detail.value) {
    case "已关注":
      interestFlag.value = false;
      jbStatus.value = 1;
      geData();
      break;
    case "已发榜":
      jbStatus.value = 4;
      geData();
      break;
    case "已揭榜":
      jbStatus.value = 0;
      geData();
      break;
    case "我要揭榜":
      jbStatus.value = 2;
      geData();
      break;
    default:
      break;
  }
}

function onCard(data) {
  console.log(data);
  uni.navigateTo({
    url: `/package_mutual_assistance/pages/mutual-assistance/classification?type=${data}`,
  });
}
onNavigationBarButtonTap((data) => {
  console.log(data);
  let a = document.querySelector(
    ".uni-page-head-ft .uni-page-head-btn .uni-btn-icon"
  ).innerHTML;
  console.log(a);
  if (a == "发布") {
    uni.navigateTo({
      url: "/package_mutual_assistance/pages/mutual-assistance/release-requirements",
    });
  } else {
    getdatasearch(searchValue.value.text);
  }
});
function getAtention(data) {
  console.log(data, ".......");
  saveHxbUserOperation({
    publishId: data,
    operationType: 1,
  }).then((res) => {
    uni.showToast({
      title: "关注成功",
      duration: 2000,
    });
  });
}
function geData() {
  isLoading.value = false;
  getXhBGroupByList({
    size: 5,
    jbStatus: jbStatus.value,
    page: curPage.value,
  }).then((res) => {
    totalPage.value = res.data.pages;
    let result = res.data.records;
    console.log(res.data.records, "..........");
    if (result.length == 0) {
      if (curPage.value - 1 >= 1) {
        curPage.value = curPage.value - 1;
      } else {
        curPage.value = 1;
      }
      uni.showToast({
        title: "没有更多数据了",
        icon: "none",
        duration: 1000,
      });
    }
    isLoading.value = true;
    result.map((o) => {
      if (o.xhbPublishPersonInfoVoList) {
        nveilingInf.value.push({
          showFlag: o.requestType,
          publishPersonId: o.id,
          title: o.title,
          unveilingText: o.description,
          unveilingAddress: o.location,
          enterpriceUrl: o.avatar,
          imageUrl: o.resourceUrlList
            ? o.resourceUrlList[0]
            : o.resourceUrlList,
          enterpriceTitle: o.xhbPublishPersonInfoVoList
            ? o.xhbPublishPersonInfoVoList[0].publishName
            : o.xhbPublishPersonInfoVoList,
          describeList: o.xhbPublishPersonInfoVoList.map((item) => {
            return item.honorName;
          }),
        });
      } else {
        nveilingInf.value.push({
          showFlag: o.requestType,
          publishPersonId: o.id,
          title: o.title,
          unveilingText: o.description,
          unveilingAddress: o.location,
          enterpriceUrl: o.avatar,
          imageUrl: o.resourceUrlList
            ? o.resourceUrlList[0]
            : o.resourceUrlList,
          enterpriceTitle: o.xhbPublishPersonInfoVoList
            ? o.xhbPublishPersonInfoVoList[0].publishName
            : o.xhbPublishPersonInfoVoList,
        });
      }
    });
  });
}
function getdatasearch(data) {
  nveilingInf.value = [];
  isLoading.value = false;
  getXhBGroupByList({
    requestType: data,
    size: 5,
    jbStatus: 2,
    page: curPage.value,
  }).then((res) => {
    console.log(res.data, "........");
    let result = res.data.records;
    isLoading.value = true;
    result.map((o) => {
      nveilingInf.value.push({
        showFlag: o.requestType,
        publishPersonId: o.id,
        title: o.title,
        unveilingText: o.description,
        unveilingAddress: o.location,
        enterpriceUrl: o.avatar,
        imageUrl: o.resourceUrlList[0],

        enterpriceTitle: o.xhbPublishPersonInfoVoList[0].publishName,
        describeList: o.xhbPublishPersonInfoVoList.map((item) => {
          return item.honorName;
        }),
      });
    });
  });
}

function scrolltolower() {
  console.log("jiazai");
  curPage.value += curPage.value;
  if (curPage.value <= totalPage.value) {
    geData();
  } else {
    uni.showToast({
      title: "没有数据啦",
      icon: "none",
    });
  }
}

onShow(() => {
  // geData();
  checkValue.value = "我要揭榜";
});

onNavigationBarSearchInputChanged((e) => {
  console.log(e);
  searchValue.value = e;
  if (e.text) {
    document.querySelector(
      ".uni-page-head-ft .uni-page-head-btn .uni-btn-icon"
    ).innerHTML = "搜索";
  } else {
    document.querySelector(
      ".uni-page-head-ft .uni-page-head-btn .uni-btn-icon"
    ).innerHTML = "发布";
  }
});
</script>

<style lang="less" scoped>
.mutual-assistance {
  overflow: hidden;
  .card-design {
    width: 90vw;
    margin: 0 40rpx;
    border-radius: 33rpx;
    background-color: #ffffff;
    margin-top: 20rpx;
  }

  .nveiling-big-box {
    padding-left: 40rpx;
    background-color: #ffffff;
    // align-items: center;
    height: calc(100vh - 10px - 281rpx - var(--window-top));
  }

  .check-mutual {
    width: 90vw;
    margin: 10rpx 30rpx 0 30rpx;
  }
}
</style>
